package LeetCode;

import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName interview
 * @Description: TODO
 * @date ${DAT}15:43
 */
public class LeetCode125 {
    public boolean isPalindrome(String s) {
        s = s.trim();
        if (s.equals("")){
            return true;
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < s.length(); i++){
            if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z' || s.charAt(i) >= 'a' && s.charAt(i) <= 'z' || s.charAt(i) >= '0' && s.charAt(i) <= '9'){
                builder.append(s.charAt(i));
            }
        }
        char[] c = builder.toString().toLowerCase().toCharArray();
        if (c.length == 0){
            return true;
        }
        int l = 0;
        int r = c.length - 1;
        boolean result = true;
        while (l < r){
            if (c[l] != c[r]){
                return false;
            }else {
                l++;
                r--;
            }
        }
        return result;
    }


    public boolean isPalindrome2(String s) {
        return isPalindromeHelper(s, 0, s.length() - 1);
    }

    public boolean isPalindromeHelper(String s, int left, int right) {
        if (left >= right)
            return true;
        while (left < right && !Character.isLetterOrDigit(s.charAt(left)))
            left++;
        while (left < right && !Character.isLetterOrDigit(s.charAt(right)))
            right--;
        return Character.toLowerCase(s.charAt(left)) == Character.toLowerCase(s.charAt(right)) && isPalindromeHelper(s, ++left, --right);
    }

    public static void main(String[] args) {
        System.out.println(new LeetCode125().isPalindrome("aa"));
    }
}
